﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Net;
using System.ServiceModel.Web;
using ScrumFactory.Services.AuthProviders;


namespace ScrumFactory.Services.Logic.AuthProviders {

    [Export(typeof(IWindowsTokenStore))]
    public class WindowsTokenStore : ScrumFactory.Services.AuthProviders.IWindowsTokenStore {

        private static List<WindowsToken> tokens = new System.Collections.Generic.List<WindowsToken>();

        public string CreateTokenFor(string user) {

            WindowsToken token = tokens.SingleOrDefault(t => t.MemberUId == user);
            if (token != null)
                return token.Token;

            token = new WindowsToken() { Token = Guid.NewGuid().ToString(), MemberUId = user };
            tokens.Add(token);

            return token.Token;

        }

        public MemberProfile ValidateToken(string token) {

            WindowsToken wtoken = tokens.SingleOrDefault(t => t.Token==token);

            if (wtoken == null)
                return null;

            MemberProfile member = new MemberProfile();
            member.MemberUId = wtoken.MemberUId;

            return member;

        }



    }

    class WindowsToken {
        public string Token { get; set; }
        public string MemberUId { get; set; }
    }
}
